﻿ using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;


namespace BrickBreaker.Gameplay
{
    //enum GameState { CLASSIC, TIMED, ENDLESS, PvP, PvC, MISC };

    public class BBGameController //: DrawableGameComponent
    {
        #region DATA MEMBERS
        //
        //private static BBILevel game;
        //private BBLevelController levelConrol;
        //private static GameState gameState;
        private bool wonGame;
        private bool lostGame;

        private BBLevelController levelControl;
        //
        #endregion DATA MEMBERS

        #region PROPERTIES

        public bool HasWonGame
        {
            get { return wonGame; }
        }

        public bool HasLostGame
        {
            get { return lostGame; }
        }

        #endregion

        #region OBJECT CONSTRUCTION
        //
        private BBGameController() { /*Private Constructor*/ }

        //Making sure ONLY 1 copy of 'BBGameController' is created...
        //Instantiate privately
        private static BBGameController controller = null;

        /// <summary>
        /// Ensures there is only once instance of the game controller
        /// </summary>
        public static BBGameController Instance()
        {
            //Singleton Check - lazy instantiation...
            if (null == controller)
            {
                controller = new BBGameController();
            }

            //Hard code 'DEFAULT' state...
            //game = BBLevelController.Instance();
            BBGlobalConfig.gameMode = GameModes.CLASSIC;
            return controller;
        }
        //
        #endregion OBJECT CONSTRUCTION
       


        #region PUBLIC METHODS
        //
        //BBLevelController is a Singleton obj, therefore 
        //it's instance variable needs to be called in a
        //NON_STATIC method...
        //public void Start()
        //{
            //bool success = levelControl.NextLevel(BBGlobalConfig.gameMode);  
        //}
        
        /// <summary>
        /// Loads the content of the game
        /// </summary>
        public void LoadContent()
        {
            levelControl = BBLevelController.Instance();
            
            BBGlobalConfig.LoadContent();
            levelControl.LoadContent();
            
            //implement later...
            //game.LoadContent();
        }
        //
        /// <summary>
        /// Unloads the content of the game and releases the memory back to the buffer.
        /// </summary>
        public void UnloadContent()
        {
            levelControl.UnloadContent();
            //implement later...
            //game.UnloadContent();
        }

        /// <summary>
        /// Switch game mode based on selection in Menu.
        /// Tracked via 'GameState' in BBGlobalController
        /// </summary>
        /// <param name="gState">The selected game mode.</param>
        public void Request(GameModes gState)
        {
            switch ((int)gState)
            {
                case 0: //GameState.CLASSIC
                case 1:
                case 2:
                case 3:
                case 4:
                case 5:
                    levelControl.NextLevel(GameModes.CLASSIC);
                    break;
                //case 1: //GameState.TIMED
                //game = BBTimedGame.Instance();
                //break;
                //case 2: //GameState.ENDLESS
                //game = BBEndlessGame.Instance();
                //break;
                //case 3: //GameState.PvP
                //game = BBPvPGame.Instance();
                //break;
                //case 4: //GameState.PvC
                //game = BBPvCGame.Instance();
                //break;
                //case 5: //GameState.MISC
                //game = BBMISCGame.Instance();
            }
        }

        /// <summary>
        /// Calls the game to move all its movable objects.
        /// </summary>
        ////public void Move(/*key, gameTime*/)
        ////{
        ////    //implement later...
        ////    game.Move();
        ////}

        /// <summary>
        /// Updates the game.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        public void Update(GameTime gameTime)
        {
            BBGlobalConfig.keyboard.Update();
            
            if (levelControl.IsGameOver)
            {
                // Show Gameover screen
                BBGlobalConfig.programState = ProgramState.MENU;
                BBGlobalConfig.menuState = MenuState.MAIN;
                levelControl.ResetLevels();
            }
            else if (!levelControl.AreAllLevelsCompleted)
            {
                levelControl.Update(gameTime);
            }
            else if(levelControl.AreAllLevelsCompleted)
            {
                // Show Congratulations screen
                BBGlobalConfig.programState = ProgramState.MENU;
                BBGlobalConfig.menuState = MenuState.MAIN;
                levelControl.ResetLevels();
            }
        }

        /// <summary>
        /// Draws the game.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        public void Draw(SpriteBatch spriteBatch)
        {
            levelControl.Draw(spriteBatch);
        }
        //
        #endregion PUBLIC METHODS

    }//end class
}//end namespace